import 'package:flutter/material.dart';
import 'flutter_cupertino_date_picker.dart';
import 'package:date_format/date_format.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: date_picker()
      //MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}


class date_picker extends StatefulWidget {
  @override
  _date_pickerState createState() => _date_pickerState();
}

class _date_pickerState extends State<date_picker> {
  DateTime _selectedDateTime = DateTime.now();
  @override
  Widget build(BuildContext context) {
    void _showDatePicker() {
      DatePicker.showDatePicker(
        context,
        pickerTheme: DateTimePickerTheme(
            showTitle: true,
            title_str:'请选择开始日期',
            titleHeight: 55,
            pickerHeight: 285,
            backgroundColor: Colors.white
        ),
        minDateTime: DateTime.parse("2010-07-12"), //选择器上可选择的最早时间
        maxDateTime: DateTime.parse("2021-11-25"), //选择器上可选择的最晚时间
        initialDateTime: _selectedDateTime, //选择器的当前选中时间
        dateFormat: "yyyy年 MMMM月 dd日", //时间格式
        locale: DateTimePickerLocale.zh_cn, //国际化配置
        onClose: () => print("----- onClose -----"),
        onCancel: () => print('onCancel'),
        onChange: (dateTime, List<int> index) {
          setState(() {
            _selectedDateTime = dateTime;
          });
        },
        onConfirm: (dateTime, List<int> index) {
          setState(() {
            print('选中日期显示----$dateTime');
            _selectedDateTime = dateTime;
          });
        },
      );
    }

    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              InkWell(
                onTap: () {
                  _showDatePicker();
                },
                child: Row(
                  children: <Widget>[
                    Text(formatDate(
                        _selectedDateTime, [yyyy, "-", mm, "-", dd])
                    )
                  ],
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
